/*
Do-file to create sibling order dataset from the two first waves (1999 and 2004)
of the Swiss household panel 1999-2013.
*/

clear

*******************************************************************
*Generate sibling order file based on the first sample (from 1999)*
*******************************************************************

***Read in 1999 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W1_1999\shp99_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age99, -10, -1)

gen occ = .
replace occ = idpers if relarp99==1
bysort idhous99: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex99 if relarp99==1
bysort idhous99: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age99 if relarp99==1
bysort idhous99: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp99, 2, 3)
bysort idhous99: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex99 if inrange(relarp99, 2, 3)
bysort idhous99: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age99 if inrange(relarp99, 2, 3)
bysort idhous99: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp99==4
keep idpers idhous99 sex99 age99 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex99 sex_kid
rename age99 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W1_1999\shp99_p_user.dta", keepusing(p99d40 p99d41 p99d45 p99d46 p99d50 p99d51 p99d55 p99d56 p99d60 p99d61 p99d65 p99d66 p99d70 p99d71)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 40 45 50 55 60 65 70 {
replace p99d`x' = . if inrange(p99d`x', -3, -1)
replace p99d`x' = 1999 - p99d`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p99d40 age1
replace age1 = . if p99d41!=1

rename p99d45 age2
replace age2 = . if p99d46!=1

rename p99d50 age3
replace age3 = . if p99d51!=1

rename p99d55 age4
replace age4 = . if p99d56!=1

rename p99d60 age5
replace age5 = . if p99d61!=1

rename p99d65 age6
replace age6 = . if p99d66!=1

rename p99d70 age7
replace age7 = . if p99d71!=1

drop p99d41 p99d46 p99d51 p99d56 p99d61 p99d66 p99d71

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous99: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous99 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous99: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc


***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7
bysort idhous99: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)7 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous99: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)8 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous99) minmode
}
*
drop occ*

foreach num of numlist 1(1)7 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)15 {
egen occ`num' = rowmax(sib1-sib8 xage1-xage7)
foreach num1 of numlist 1(1)7 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)8 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ9-occ15 sib1-sib8 xage1-xage7



foreach x of numlist 1(1)7 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age7

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1


***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 1999


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous99 idhous
replace age_kid = 1999-age_kid
replace age_ref = 1999-age_ref
replace age_spouse = 1999-age_spouse
replace age_mother = 1999-age_mother
replace age_father = 1999-age_father
replace occ1 = occ1+14
replace occ2 = occ2+14
replace occ3 = occ3+14
replace occ4 = occ4+14
replace occ5 = occ5+14
replace occ6 = occ6+14
replace occ7 = occ7+14
replace occ8 = occ8+14
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ8


***Add outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W1_1999\shp99_p_user.dta", keepusing(p99p01 p99p03 p99p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p99p06 = . if inrange(p99p06, -5, -1)
rename p99p06 turnout_kid



***Generate indicator for external efficacy
replace p99p03 = . if inrange(p99p03, -5, -1)
rename p99p03 ext_eff_kid


***Generate indicator for political interest
replace p99p01 = . if inrange(p99p01, -3, -1)
rename p99p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling1999.dta", replace


****************************************************
*Generate sibling order file based on the 2000 data*
****************************************************

***Read in 2000 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W2_2000\shp00_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age00, -10, -1)

gen occ = .
replace occ = idpers if relarp00==1
bysort idhous00: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex00 if relarp00==1
bysort idhous00: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age00 if relarp00==1
bysort idhous00: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp00, 2, 3)
bysort idhous00: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex00 if inrange(relarp00, 2, 3)
bysort idhous00: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age00 if inrange(relarp00, 2, 3)
bysort idhous00: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp00==4
keep idpers idhous00 sex00 age00 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex00 sex_kid
rename age00 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W2_2000\shp00_p_user.dta", keepusing(p00d40 p00d41 p00d45 p00d46 p00d50 p00d51 p00d55 p00d56 p00d60 p00d61 p00d65 p00d66 p00d70 p00d71)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 40 45 50 55 60 65 70 {
replace p00d`x' = . if inrange(p00d`x', -3, -1)
replace p00d`x' = 2000 - p00d`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p00d40 age1
replace age1 = . if p00d41!=1

rename p00d45 age2
replace age2 = . if p00d46!=1

rename p00d50 age3
replace age3 = . if p00d51!=1

rename p00d55 age4
replace age4 = . if p00d56!=1

rename p00d60 age5
replace age5 = . if p00d61!=1

rename p00d65 age6
replace age6 = . if p00d66!=1

rename p00d70 age7
replace age7 = . if p00d71!=1

drop p00d41 p00d46 p00d51 p00d56 p00d61 p00d66 p00d71

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous00: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous00 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous00: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc


***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7
bysort idhous00: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)7 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous00: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)7 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous00) minmode
}
*
drop occ*

foreach num of numlist 1(1)7 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)14 {
egen occ`num' = rowmax(sib1-sib7 xage1-xage7)
foreach num1 of numlist 1(1)7 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)7 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ8-occ14 sib1-sib7 xage1-xage7



foreach x of numlist 1(1)7 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age7

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1


***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2000


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous00 idhous
replace age_kid = 2000-age_kid
replace age_ref = 2000-age_ref
replace age_spouse = 2000-age_spouse
replace age_mother = 2000-age_mother
replace age_father = 2000-age_father
replace occ1 = occ1+13
replace occ2 = occ2+13
replace occ3 = occ3+13
replace occ4 = occ4+13
replace occ5 = occ5+13
replace occ6 = occ6+13
replace occ7 = occ7+13
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ7


**Add outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W2_2000\shp00_p_user.dta", keepusing(p00p01 p00p03 p00p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid



***Generate indicator for turnout
replace p00p06 = . if inrange(p00p06, -5, -1)
rename p00p06 turnout_kid



***Generate indicator for external efficacy
replace p00p03 = . if inrange(p00p03, -5, -1)
rename p00p03 ext_eff_kid


***Generate indicator for political interest
replace p00p01 = . if inrange(p00p01, -3, -1)
rename p00p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2000.dta", replace




****************************************************
*Generate sibling order file based on the 2001 data*
****************************************************

***Read in 2001 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W3_2001\shp01_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age01, -10, -1)

gen occ = .
replace occ = idpers if relarp01==1
bysort idhous01: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex01 if relarp01==1
bysort idhous01: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age01 if relarp01==1
bysort idhous01: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp01, 2, 3)
bysort idhous01: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex01 if inrange(relarp01, 2, 3)
bysort idhous01: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age01 if inrange(relarp01, 2, 3)
bysort idhous01: egen age_spouse = mode(occ)
***Drop three spouses who are aged 0
drop if age_spouse==0
drop occ


***Keep only sons and daughters
keep if relarp01==4
keep idpers idhous01 sex01 age01 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex01 sex_kid
rename age01 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W3_2001\shp01_p_user.dta", keepusing(p01d40 p01d41 p01d45 p01d46 p01d50 p01d51 p01d55 p01d56 p01d60 p01d61 p01d65 p01d66 p01d70 p01d71)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 40 45 50 55 60 65 70 {
replace p01d`x' = . if inrange(p01d`x', -3, -1)
replace p01d`x' = 2001 - p01d`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p01d40 age1
replace age1 = . if p01d41!=1

rename p01d45 age2
replace age2 = . if p01d46!=1

rename p01d50 age3
replace age3 = . if p01d51!=1

rename p01d55 age4
replace age4 = . if p01d56!=1

rename p01d60 age5
replace age5 = . if p01d61!=1

rename p01d65 age6
replace age6 = . if p01d66!=1

rename p01d70 age7
replace age7 = . if p01d71!=1

drop p01d41 p01d46 p01d51 p01d56 p01d61 p01d66 p01d71

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous01: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous01 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous01: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7
bysort idhous01: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)7 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous01: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)7 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous01) minmode
}
*
drop occ*

foreach num of numlist 1(1)7 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)14 {
egen occ`num' = rowmax(sib1-sib7 xage1-xage7)
foreach num1 of numlist 1(1)7 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)7 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ8-occ14 sib1-sib7 xage1-xage7



foreach x of numlist 1(1)7 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age7

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1


***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2001


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous01 idhous
replace age_kid = 2001-age_kid
replace age_ref = 2001-age_ref
replace age_spouse = 2001-age_spouse
replace age_mother = 2001-age_mother
replace age_father = 2001-age_father
replace occ1 = occ1+12
replace occ2 = occ2+12
replace occ3 = occ3+12
replace occ4 = occ4+12
replace occ5 = occ5+12
replace occ6 = occ6+12
replace occ7 = occ7+12
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ7




***Add outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W3_2001\shp01_p_user.dta", keepusing(p01p01 p01p03 p01p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p01p06 = . if inrange(p01p06, -5, -1)
rename p01p06 turnout_kid



***Generate indicator for external efficacy
replace p01p03 = . if inrange(p01p03, -5, -1)
rename p01p03 ext_eff_kid


***Generate indicator for political interest
replace p01p01 = . if inrange(p01p01, -3, -1)
rename p01p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2001.dta", replace




****************************************************
*Generate sibling order file based on the 2002 data*
****************************************************

***Read in 2002 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W4_2002\shp02_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age02, -10, -1)

gen occ = .
replace occ = idpers if relarp02==1
bysort idhous02: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex02 if relarp02==1
bysort idhous02: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age02 if relarp02==1
bysort idhous02: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp02, 2, 3)
bysort idhous02: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex02 if inrange(relarp02, 2, 3)
bysort idhous02: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age02 if inrange(relarp02, 2, 3)
bysort idhous02: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp02==4
keep idpers idhous02 sex02 age02 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex02 sex_kid
rename age02 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W4_2002\shp02_p_user.dta", keepusing(p02d40 p02d41 p02d45 p02d46 p02d50 p02d51 p02d55 p02d56 p02d60 p02d61 p02d65 p02d66 p02d70 p02d71)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 40 45 50 55 60 65 70 {
replace p02d`x' = . if inrange(p02d`x', -3, -1)
replace p02d`x' = 2002 - p02d`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p02d40 age1
replace age1 = . if p02d41!=1

rename p02d45 age2
replace age2 = . if p02d46!=1

rename p02d50 age3
replace age3 = . if p02d51!=1

rename p02d55 age4
replace age4 = . if p02d56!=1

rename p02d60 age5
replace age5 = . if p02d61!=1

rename p02d65 age6
replace age6 = . if p02d66!=1

rename p02d70 age7
replace age7 = . if p02d71!=1

drop p02d41 p02d46 p02d51 p02d56 p02d61 p02d66 p02d71

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous02: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous02 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous02: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7
bysort idhous02: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)7 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous02: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)6 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous02) minmode
}
*
drop occ*

foreach num of numlist 1(1)7 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)13 {
egen occ`num' = rowmax(sib1-sib6 xage1-xage7)
foreach num1 of numlist 1(1)7 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)6 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ7-occ13 sib1-sib6 xage1-xage7



foreach x of numlist 1(1)7 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age7

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1


***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2002


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous02 idhous
replace age_kid = 2002-age_kid
replace age_ref = 2002-age_ref
replace age_spouse = 2002-age_spouse
replace age_mother = 2002-age_mother
replace age_father = 2002-age_father
replace occ1 = occ1+11
replace occ2 = occ2+11
replace occ3 = occ3+11
replace occ4 = occ4+11
replace occ5 = occ5+11
replace occ6 = occ6+11
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ6


***Add outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W4_2002\shp02_p_user.dta", keepusing(p02p01 p02p03 p02p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p02p06 = . if inrange(p02p06, -5, -1)
rename p02p06 turnout_kid



***Generate indicator for external efficacy
replace p02p03 = . if inrange(p02p03, -5, -1)
rename p02p03 ext_eff_kid


***Generate indicator for political interest
replace p02p01 = . if inrange(p02p01, -3, -1)
rename p02p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2002.dta", replace




****************************************************
*Generate sibling order file based on the 2003 data*
****************************************************

***Read in 2003 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W5_2003\shp03_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age03, -10, -1)

gen occ = .
replace occ = idpers if relarp03==1
bysort idhous03: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex03 if relarp03==1
bysort idhous03: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age03 if relarp03==1
bysort idhous03: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp03, 2, 3)
bysort idhous03: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex03 if inrange(relarp03, 2, 3)
bysort idhous03: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age03 if inrange(relarp03, 2, 3)
bysort idhous03: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp03==4
keep idpers idhous03 sex03 age03 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex03 sex_kid
rename age03 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W5_2003\shp03_p_user.dta", keepusing(p03d40 p03d41 p03d45 p03d46 p03d50 p03d51 p03d55 p03d56 p03d60 p03d61 p03d65 p03d66 p03d70 p03d71)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 40 45 50 55 60 65 70 {
replace p03d`x' = . if inrange(p03d`x', -3, -1)
replace p03d`x' = 2003 - p03d`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p03d40 age1
replace age1 = . if p03d41!=1

rename p03d45 age2
replace age2 = . if p03d46!=1

rename p03d50 age3
replace age3 = . if p03d51!=1

rename p03d55 age4
replace age4 = . if p03d56!=1

rename p03d60 age5
replace age5 = . if p03d61!=1

rename p03d65 age6
replace age6 = . if p03d66!=1

rename p03d70 age7
replace age7 = . if p03d71!=1

drop p03d41 p03d46 p03d51 p03d56 p03d61 p03d66 p03d71

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous03: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous03 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous03: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7
bysort idhous03: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)7 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous03: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)5 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous03) minmode
}
*
drop occ*

foreach num of numlist 1(1)7 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)12 {
egen occ`num' = rowmax(sib1-sib5 xage1-xage7)
foreach num1 of numlist 1(1)7 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)5 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ6-occ12 sib1-sib5 xage1-xage7



foreach x of numlist 1(1)7 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age7

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1


***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2003


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous03 idhous
replace age_kid = 2003-age_kid
replace age_ref = 2003-age_ref
replace age_spouse = 2003-age_spouse
replace age_mother = 2003-age_mother
replace age_father = 2003-age_father
replace occ1 = occ1+10
replace occ2 = occ2+10
replace occ3 = occ3+10
replace occ4 = occ4+10
replace occ5 = occ5+10
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ5


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W5_2003\shp03_p_user.dta", keepusing(p03p01 p03p03 p03p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid



***Generate indicator for turnout
replace p03p06 = . if inrange(p03p06, -5, -1)
rename p03p06 turnout_kid



***Generate indicator for external efficacy
replace p03p03 = . if inrange(p03p03, -5, -1)
rename p03p03 ext_eff_kid


***Generate indicator for political interest
replace p03p01 = . if inrange(p03p01, -3, -1)
rename p03p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2003.dta", replace





****************************************************
*Generate sibling order file based on the 2004 data*
****************************************************

***Read in 2004 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W6_2004\shp04_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age04, -10, -1)

gen occ = .
replace occ = idpers if relarp04==1
bysort idhous04: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex04 if relarp04==1
bysort idhous04: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age04 if relarp04==1
bysort idhous04: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp04, 2, 3)
bysort idhous04: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex04 if inrange(relarp04, 2, 3)
bysort idhous04: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age04 if inrange(relarp04, 2, 3)
bysort idhous04: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp04==4
keep idpers idhous04 sex04 age04 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex04 sex_kid
rename age04 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W6_2004\shp04_p_user.dta", keepusing(p04d113 p04d114 p04d118 p04d119 p04d123 p04d124 p04d128 p04d129 p04d133 p04d134 p04d138 p04d139 p04d143 p04d144 p04d148 p04d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p04d1`x' = . if inrange(p04d1`x', -3, -1)
replace p04d1`x' = 2004 - p04d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p04d113 age1
replace age1 = . if p04d114!=1

rename p04d118 age2
replace age2 = . if p04d119!=1

rename p04d123 age3
replace age3 = . if p04d124!=1

rename p04d128 age4
replace age4 = . if p04d129!=1

rename p04d133 age5
replace age5 = . if p04d134!=1

rename p04d138 age6
replace age6 = . if p04d139!=1

rename p04d143 age7
replace age7 = . if p04d144!=1

rename p04d148 age8
replace age8 = . if p04d149!=1

drop p04d114 p04d119 p04d124 p04d129 p04d134 p04d139 p04d144 p04d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous04: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous04 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous04: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous04: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous04: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)7 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous04) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)15 {
egen occ`num' = rowmax(sib1-sib7 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)7 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ8-occ15 sib1-sib7 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2004


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous04 idhous
replace age_kid = 2004-age_kid
replace age_ref = 2004-age_ref
replace age_spouse = 2004-age_spouse
replace age_mother = 2004-age_mother
replace age_father = 2004-age_father
replace occ1 = occ1+9
replace occ2 = occ2+9
replace occ3 = occ3+9
replace occ4 = occ4+9
replace occ5 = occ5+9
replace occ6 = occ6+9
replace occ7 = occ7+9
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ7


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W6_2004\shp04_p_user.dta", keepusing(p04p01 p04p03 p04p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid



***Generate indicator for turnout
replace p04p06 = . if inrange(p04p06, -5, -1)
rename p04p06 turnout_kid


***Generate indicator for external efficacy
replace p04p03 = . if inrange(p04p03, -5, -1)
rename p04p03 ext_eff_kid


***Generate indicator for political interest
replace p04p01 = . if inrange(p04p01, -3, -1)
rename p04p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2004.dta", replace





****************************************************
*Generate sibling order file based on the 2005 data*
****************************************************

***Read in 2005 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W7_2005\shp05_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age05, -10, -1)

gen occ = .
replace occ = idpers if relarp05==1
bysort idhous05: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex05 if relarp05==1
bysort idhous05: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age05 if relarp05==1
bysort idhous05: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp05, 2, 3)
bysort idhous05: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex05 if inrange(relarp05, 2, 3)
bysort idhous05: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age05 if inrange(relarp05, 2, 3)
bysort idhous05: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp05==4
keep idpers idhous05 sex05 age05 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex05 sex_kid
rename age05 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W7_2005\shp05_p_user.dta", keepusing(p05d113 p05d114 p05d118 p05d119 p05d123 p05d124 p05d128 p05d129 p05d133 p05d134 p05d138 p05d139 p05d143 p05d144 p05d148 p05d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p05d1`x' = . if inrange(p05d1`x', -3, -1)
replace p05d1`x' = 2005 - p05d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p05d113 age1
replace age1 = . if p05d114!=1

rename p05d118 age2
replace age2 = . if p05d119!=1

rename p05d123 age3
replace age3 = . if p05d124!=1

rename p05d128 age4
replace age4 = . if p05d129!=1

rename p05d133 age5
replace age5 = . if p05d134!=1

rename p05d138 age6
replace age6 = . if p05d139!=1

rename p05d143 age7
replace age7 = . if p05d144!=1

rename p05d148 age8
replace age8 = . if p05d149!=1

drop p05d114 p05d119 p05d124 p05d129 p05d134 p05d139 p05d144 p05d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous05: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous05 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous05: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous05: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous05: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)5 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous05) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)13 {
egen occ`num' = rowmax(sib1-sib5 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)5 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ6-occ13 sib1-sib5 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2005


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous05 idhous
replace age_kid = 2005-age_kid
replace age_ref = 2005-age_ref
replace age_spouse = 2005-age_spouse
replace age_mother = 2005-age_mother
replace age_father = 2005-age_father
replace occ1 = occ1+8
replace occ2 = occ2+8
replace occ3 = occ3+8
replace occ4 = occ4+8
replace occ5 = occ5+8
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ5


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W7_2005\shp05_p_user.dta", keepusing(p05p01 p05p03 p05p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid



***Generate indicator for turnout
replace p05p06 = . if inrange(p05p06, -5, -1)
rename p05p06 turnout_kid



***Generate indicator for external efficacy
replace p05p03 = . if inrange(p05p03, -5, -1)
rename p05p03 ext_eff_kid


***Generate indicator for political interest
replace p05p01 = . if inrange(p05p01, -3, -1)
rename p05p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2005.dta", replace





****************************************************
*Generate sibling order file based on the 2006 data*
****************************************************

***Read in 2006 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W8_2006\shp06_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
gen occ = .
drop if inrange(age06, -10, -1)

replace occ = idpers if relarp06==1
bysort idhous06: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex06 if relarp06==1
bysort idhous06: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age06 if relarp06==1
bysort idhous06: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp06, 2, 3)
bysort idhous06: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex06 if inrange(relarp06, 2, 3)
bysort idhous06: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age06 if inrange(relarp06, 2, 3)
bysort idhous06: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp06==4
keep idpers idhous06 sex06 age06 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex06 sex_kid
rename age06 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W8_2006\shp06_p_user.dta", keepusing(p06d113 p06d114 p06d118 p06d119 p06d123 p06d124 p06d128 p06d129 p06d133 p06d134 p06d138 p06d139 p06d143 p06d144 p06d148 p06d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p06d1`x' = . if inrange(p06d1`x', -3, -1)
replace p06d1`x' = 2006 - p06d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p06d113 age1
replace age1 = . if p06d114!=1

rename p06d118 age2
replace age2 = . if p06d119!=1

rename p06d123 age3
replace age3 = . if p06d124!=1

rename p06d128 age4
replace age4 = . if p06d129!=1

rename p06d133 age5
replace age5 = . if p06d134!=1

rename p06d138 age6
replace age6 = . if p06d139!=1

rename p06d143 age7
replace age7 = . if p06d144!=1

rename p06d148 age8
replace age8 = . if p06d149!=1

drop p06d114 p06d119 p06d124 p06d129 p06d134 p06d139 p06d144 p06d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous06: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous06 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous06: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous06: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous06: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)5 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous06) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)13 {
egen occ`num' = rowmax(sib1-sib5 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)5 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ6-occ13 sib1-sib5 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2006


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous06 idhous
replace age_kid = 2006-age_kid
replace age_ref = 2006-age_ref
replace age_spouse = 2006-age_spouse
replace age_mother = 2006-age_mother
replace age_father = 2006-age_father
replace occ1 = occ1+7
replace occ2 = occ2+7
replace occ3 = occ3+7
replace occ4 = occ4+7
replace occ5 = occ5+7
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ5


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W8_2006\shp06_p_user.dta", keepusing(p06p01 p06p03 p06p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p06p06 = . if inrange(p06p06, -5, -1)
rename p06p06 turnout_kid


***Generate indicator for external efficacy
replace p06p03 = . if inrange(p06p03, -5, -1)
rename p06p03 ext_eff_kid


***Generate indicator for political interest
replace p06p01 = . if inrange(p06p01, -3, -1)
rename p06p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2006.dta", replace





****************************************************
*Generate sibling order file based on the 2007 data*
****************************************************

***Read in 2007 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W9_2007\shp07_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age07, -10, -1)

gen occ = .
replace occ = idpers if relarp07==1
bysort idhous07: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex07 if relarp07==1
bysort idhous07: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age07 if relarp07==1
bysort idhous07: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp07, 2, 3)
bysort idhous07: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex07 if inrange(relarp07, 2, 3)
bysort idhous07: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age07 if inrange(relarp07, 2, 3)
bysort idhous07: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp07==4
keep idpers idhous07 sex07 age07 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex07 sex_kid
rename age07 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W9_2007\shp07_p_user.dta", keepusing(p07d113 p07d114 p07d118 p07d119 p07d123 p07d124 p07d128 p07d129 p07d133 p07d134 p07d138 p07d139 p07d143 p07d144 p07d148 p07d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p07d1`x' = . if inrange(p07d1`x', -3, -1)
replace p07d1`x' = 2007 - p07d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p07d113 age1
replace age1 = . if p07d114!=1

rename p07d118 age2
replace age2 = . if p07d119!=1

rename p07d123 age3
replace age3 = . if p07d124!=1

rename p07d128 age4
replace age4 = . if p07d129!=1

rename p07d133 age5
replace age5 = . if p07d134!=1

rename p07d138 age6
replace age6 = . if p07d139!=1

rename p07d143 age7
replace age7 = . if p07d144!=1

rename p07d148 age8
replace age8 = . if p07d149!=1

drop p07d114 p07d119 p07d124 p07d129 p07d134 p07d139 p07d144 p07d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous07: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous07 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous07: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous07: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous07: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)8 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous07) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)16 {
egen occ`num' = rowmax(sib1-sib8 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)8 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ9-occ16 sib1-sib8 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2007


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous07 idhous
replace age_kid = 2007-age_kid
replace age_ref = 2007-age_ref
replace age_spouse = 2007-age_spouse
replace age_mother = 2007-age_mother
replace age_father = 2007-age_father
replace occ1 = occ1+6
replace occ2 = occ2+6
replace occ3 = occ3+6
replace occ4 = occ4+6
replace occ5 = occ5+6
replace occ6 = occ6+6
replace occ7 = occ7+6
replace occ8 = occ8+6
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ8


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W9_2007\shp07_p_user.dta", keepusing(p07p01 p07p03 p07p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p07p06 = . if inrange(p07p06, -5, -1)
rename p07p06 turnout_kid


***Generate indicator for external efficacy
replace p07p03 = . if inrange(p07p03, -5, -1)
rename p07p03 ext_eff_kid


***Generate indicator for political interest
replace p07p01 = . if inrange(p07p01, -3, -1)
rename p07p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2007.dta", replace






****************************************************
*Generate sibling order file based on the 2008 data*
****************************************************

***Read in 2008 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W10_2008\shp08_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age08, -10, -1)

gen occ = .
replace occ = idpers if relarp08==1
bysort idhous08: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex08 if relarp08==1
bysort idhous08: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age08 if relarp08==1
bysort idhous08: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp08, 2, 3)
bysort idhous08: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex08 if inrange(relarp08, 2, 3)
bysort idhous08: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age08 if inrange(relarp08, 2, 3)
bysort idhous08: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp08==4
keep idpers idhous08 sex08 age08 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex08 sex_kid
rename age08 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W10_2008\shp08_p_user.dta", keepusing(p08d113 p08d114 p08d118 p08d119 p08d123 p08d124 p08d128 p08d129 p08d133 p08d134 p08d138 p08d139 p08d143 p08d144 p08d148 p08d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p08d1`x' = . if inrange(p08d1`x', -3, -1)
replace p08d1`x' = 2008 - p08d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p08d113 age1
replace age1 = . if p08d114!=1

rename p08d118 age2
replace age2 = . if p08d119!=1

rename p08d123 age3
replace age3 = . if p08d124!=1

rename p08d128 age4
replace age4 = . if p08d129!=1

rename p08d133 age5
replace age5 = . if p08d134!=1

rename p08d138 age6
replace age6 = . if p08d139!=1

rename p08d143 age7
replace age7 = . if p08d144!=1

rename p08d148 age8
replace age8 = . if p08d149!=1

drop p08d114 p08d119 p08d124 p08d129 p08d134 p08d139 p08d144 p08d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous08: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous08 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous08: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous08: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous08: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)7 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous08) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)15 {
egen occ`num' = rowmax(sib1-sib7 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)7 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ8-occ15 sib1-sib7 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2008


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous08 idhous
replace age_kid = 2008-age_kid
replace age_ref = 2008-age_ref
replace age_spouse = 2008-age_spouse
replace age_mother = 2008-age_mother
replace age_father = 2008-age_father
replace occ1 = occ1+5
replace occ2 = occ2+5
replace occ3 = occ3+5
replace occ4 = occ4+5
replace occ5 = occ5+5
replace occ6 = occ6+5
replace occ7 = occ7+5
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ7


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W10_2008\shp08_p_user.dta", keepusing(p08p01 p08p03 p08p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p08p06 = . if inrange(p08p06, -5, -1)
rename p08p06 turnout_kid


***Generate indicator for external efficacy
replace p08p03 = . if inrange(p08p03, -5, -1)
rename p08p03 ext_eff_kid


***Generate indicator for political interest
replace p08p01 = . if inrange(p08p01, -3, -1)
rename p08p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2008.dta", replace






****************************************************
*Generate sibling order file based on the 2009 data*
****************************************************

***Read in 2009 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W11_2009\shp09_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age09, -10, -1)

gen occ = .
replace occ = idpers if relarp09==1
bysort idhous09: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex09 if relarp09==1
bysort idhous09: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age09 if relarp09==1
bysort idhous09: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp09, 2, 3)
bysort idhous09: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex09 if inrange(relarp09, 2, 3)
bysort idhous09: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age09 if inrange(relarp09, 2, 3)
bysort idhous09: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp09==4
keep idpers idhous09 sex09 age09 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex09 sex_kid
rename age09 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W11_2009\shp09_p_user.dta", keepusing(p09d113 p09d114 p09d118 p09d119 p09d123 p09d124 p09d128 p09d129 p09d133 p09d134 p09d138 p09d139 p09d143 p09d144 p09d148 p09d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p09d1`x' = . if inrange(p09d1`x', -3, -1)
replace p09d1`x' = 2009 - p09d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p09d113 age1
replace age1 = . if p09d114!=1

rename p09d118 age2
replace age2 = . if p09d119!=1

rename p09d123 age3
replace age3 = . if p09d124!=1

rename p09d128 age4
replace age4 = . if p09d129!=1

rename p09d133 age5
replace age5 = . if p09d134!=1

rename p09d138 age6
replace age6 = . if p09d139!=1

rename p09d143 age7
replace age7 = . if p09d144!=1

rename p09d148 age8
replace age8 = . if p09d149!=1

drop p09d114 p09d119 p09d124 p09d129 p09d134 p09d139 p09d144 p09d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous09: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous09 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous09: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous09: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous09: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)8 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous09) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)16 {
egen occ`num' = rowmax(sib1-sib8 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)8 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ9-occ16 sib1-sib8 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2009


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous09 idhous
replace age_kid = 2009-age_kid
replace age_ref = 2009-age_ref
replace age_spouse = 2009-age_spouse
replace age_mother = 2009-age_mother
replace age_father = 2009-age_father
replace occ1 = occ1+4
replace occ2 = occ2+4
replace occ3 = occ3+4
replace occ4 = occ4+4
replace occ5 = occ5+4
replace occ6 = occ6+4
replace occ7 = occ7+4
replace occ8 = occ8+4
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ8


***Add political outcomes for the kids*
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W11_2009\shp09_p_user.dta", keepusing(p09p01 p09p03 p09p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p09p06 = . if inrange(p09p06, -5, -1)
rename p09p06 turnout_kid


***Generate indicator for external efficacy
replace p09p03 = . if inrange(p09p03, -5, -1)
rename p09p03 ext_eff_kid


***Generate indicator for political interest
replace p09p01 = . if inrange(p09p01, -3, -1)
rename p09p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2009.dta", replace






****************************************************
*Generate sibling order file based on the 2010 data*
****************************************************

***Read in 2010 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W12_2010\shp10_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age10, -10, -1)

gen occ = .
replace occ = idpers if relarp10==1
bysort idhous10: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex10 if relarp10==1
bysort idhous10: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age10 if relarp10==1
bysort idhous10: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp10, 2, 3)
bysort idhous10: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex10 if inrange(relarp10, 2, 3)
bysort idhous10: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age10 if inrange(relarp10, 2, 3)
bysort idhous10: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp10==4
keep idpers idhous10 sex10 age10 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex10 sex_kid
rename age10 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W12_2010\shp10_p_user.dta", keepusing(p10d113 p10d114 p10d118 p10d119 p10d123 p10d124 p10d128 p10d129 p10d133 p10d134 p10d138 p10d139 p10d143 p10d144 p10d148 p10d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p10d1`x' = . if inrange(p10d1`x', -3, -1)
replace p10d1`x' = 2010 - p10d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p10d113 age1
replace age1 = . if p10d114!=1

rename p10d118 age2
replace age2 = . if p10d119!=1

rename p10d123 age3
replace age3 = . if p10d124!=1

rename p10d128 age4
replace age4 = . if p10d129!=1

rename p10d133 age5
replace age5 = . if p10d134!=1

rename p10d138 age6
replace age6 = . if p10d139!=1

rename p10d143 age7
replace age7 = . if p10d144!=1

rename p10d148 age8
replace age8 = . if p10d149!=1

drop p10d114 p10d119 p10d124 p10d129 p10d134 p10d139 p10d144 p10d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous10: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous10 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous10: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous10: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous10: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)8 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous10) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)16 {
egen occ`num' = rowmax(sib1-sib8 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)8 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ9-occ16 sib1-sib8 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2010


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous10 idhous
replace age_kid = 2010-age_kid
replace age_ref = 2010-age_ref
replace age_spouse = 2010-age_spouse
replace age_mother = 2010-age_mother
replace age_father = 2010-age_father
replace occ1 = occ1+3
replace occ2 = occ2+3
replace occ3 = occ3+3
replace occ4 = occ4+3
replace occ5 = occ5+3
replace occ6 = occ6+3
replace occ7 = occ7+3
replace occ8 = occ8+3
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ8


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W12_2010\shp10_p_user.dta", keepusing(p10p01)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for political interest
replace p10p01 = . if inrange(p10p01, -3, -1)
rename p10p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2010.dta", replace






****************************************************
*Generate sibling order file based on the 2011 data*
****************************************************

***Read in 2011 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W13_2011\shp11_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age11, -10, -1)

gen occ = .
replace occ = idpers if relarp11==1
bysort idhous11: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex11 if relarp11==1
bysort idhous11: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age11 if relarp11==1
bysort idhous11: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp11, 2, 3)
bysort idhous11: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex11 if inrange(relarp11, 2, 3)
bysort idhous11: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age11 if inrange(relarp11, 2, 3)
bysort idhous11: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp11==4
keep idpers idhous11 sex11 age11 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex11 sex_kid
rename age11 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W13_2011\shp11_p_user.dta", keepusing(p11d113 p11d114 p11d118 p11d119 p11d123 p11d124 p11d128 p11d129 p11d133 p11d134 p11d138 p11d139 p11d143 p11d144 p11d148 p11d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p11d1`x' = . if inrange(p11d1`x', -3, -1)
replace p11d1`x' = 2011 - p11d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p11d113 age1
replace age1 = . if p11d114!=1

rename p11d118 age2
replace age2 = . if p11d119!=1

rename p11d123 age3
replace age3 = . if p11d124!=1

rename p11d128 age4
replace age4 = . if p11d129!=1

rename p11d133 age5
replace age5 = . if p11d134!=1

rename p11d138 age6
replace age6 = . if p11d139!=1

rename p11d143 age7
replace age7 = . if p11d144!=1

rename p11d148 age8
replace age8 = . if p11d149!=1

drop p11d114 p11d119 p11d124 p11d129 p11d134 p11d139 p11d144 p11d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous11: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous11 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous11: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous11: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous11: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)8 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous11) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)16 {
egen occ`num' = rowmax(sib1-sib8 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)8 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ9-occ16 sib1-sib8 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2011


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous11 idhous
replace age_kid = 2011-age_kid
replace age_ref = 2011-age_ref
replace age_spouse = 2011-age_spouse
replace age_mother = 2011-age_mother
replace age_father = 2011-age_father
replace occ1 = occ1+2
replace occ2 = occ2+2
replace occ3 = occ3+2
replace occ4 = occ4+2
replace occ5 = occ5+2
replace occ6 = occ6+2
replace occ7 = occ7+2
replace occ8 = occ8+2
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ8


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W13_2011\shp11_p_user.dta", keepusing(p11p01 p11p03 p11p06)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for turnout
replace p11p06 = . if inrange(p11p06, -5, -1)
rename p11p06 turnout_kid


***Generate indicator for external efficacy
replace p11p03 = . if inrange(p11p03, -5, -1)
rename p11p03 ext_eff_kid


***Generate indicator for political interest
replace p11p01 = . if inrange(p11p01, -3, -1)
rename p11p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2011.dta", replace






****************************************************
*Generate sibling order file based on the 2012 data*
****************************************************

***Read in 2012 data
use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W14_2012\shp12_p_user.dta", clear

***Generate id, sex and age for reference person and spouse to reference
drop if inrange(age12, -10, -1)

gen occ = .
replace occ = idpers if relarp12==1
bysort idhous12: egen idpers_ref = mode(occ)
drop occ

gen occ = .
replace occ = sex12 if relarp12==1
bysort idhous12: egen sex_ref = mode(occ)
drop occ

gen occ = .
replace occ = age12 if relarp12==1
bysort idhous12: egen age_ref = mode(occ)
drop occ


gen occ = .
replace occ = idpers if inrange(relarp12, 2, 3)
bysort idhous12: egen idpers_spouse = mode(occ)
drop occ

gen occ = .
replace occ = sex12 if inrange(relarp12, 2, 3)
bysort idhous12: egen sex_spouse = mode(occ)
drop occ

gen occ = .
replace occ = age12 if inrange(relarp12, 2, 3)
bysort idhous12: egen age_spouse = mode(occ)
drop occ


***Keep only sons and daughters
keep if relarp12==4
keep idpers idhous12 sex12 age12 idpers_ref idpers_spouse sex_ref sex_spouse age_ref age_spouse
rename idpers idpers_kid
rename sex12 sex_kid
rename age12 age_kid


***Merge to age of kids of reference person not living in household
rename idpers_ref idpers

merge m:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W14_2012\shp12_p_user.dta", keepusing(p12d113 p12d114 p12d118 p12d119 p12d123 p12d124 p12d128 p12d129 p12d133 p12d134 p12d138 p12d139 p12d143 p12d144 p12d148 p12d149)
keep if _merge==3
drop _merge
rename idpers idpers_ref



***Generate age of kids not living in household
foreach x in 13 18 23 28 33 38 43 48 {
replace p12d1`x' = . if inrange(p12d1`x', -3, -1)
replace p12d1`x' = 2012 - p12d1`x'
}
*


***Rename and set to missing for kids that are not children to both current household parents
rename p12d113 age1
replace age1 = . if p12d114!=1

rename p12d118 age2
replace age2 = . if p12d119!=1

rename p12d123 age3
replace age3 = . if p12d124!=1

rename p12d128 age4
replace age4 = . if p12d129!=1

rename p12d133 age5
replace age5 = . if p12d134!=1

rename p12d138 age6
replace age6 = . if p12d139!=1

rename p12d143 age7
replace age7 = . if p12d144!=1

rename p12d148 age8
replace age8 = . if p12d149!=1

drop p12d114 p12d119 p12d124 p12d129 p12d134 p12d139 p12d144 p12d149

/*Generate indicator for number of children in household
whose both parents are equal to the household parents;
*/

***Generate number of kids in household included in the sample
gen occ = 1
bysort idhous12: egen sample_kids = sum(occ)
drop occ

/*Generate number of kids in household not included in the sample;
Make sure nonsample kids only include those whose both parents are
equal to the household parents*/
egen nonsample_kids = rownonmiss(age1 age2 age3 age4 age5 age6 age7 age8)

***Generate indicator for total number of kids of current household parents/family size
gen total_kids = sample_kids + nonsample_kids



***Drop sample twin families (equal to two or more kids born the same year)
bysort idhous12 age_kid: gen occ = _n
gen nocc = 1 if inrange(occ, 2, 3)
bysort idhous12: egen mocc = mean(nocc)
drop if mocc==1
drop occ nocc mocc

***Drop remaining nonsample twin families (equal to two or more kids born the same year)
gen occ = 1 if age_kid==age1 | age_kid==age2 | age_kid==age3 | age_kid==age4 | age_kid==age5 | age_kid==age6 | age_kid==age7 | age_kid==age8
bysort idhous12: egen nocc = mean(occ)
drop if nocc==1
drop occ nocc


/*Generate indicator for sibling order and family type taking nomsample kids into account;
Only take nonsample kids into account that are older than 16 under the assumption
younger nonsample kids who are biological children to the household parents
are probably adopted away.
*/

foreach num of numlist 1(1)8 {
replace age`num' = . if inrange(age`num', 0, 16)
}
*

bysort idhous12: egen siblingorder = rank(age_kid), field


***Prepare variables to be used for family type indicator
foreach num of numlist 1(1)6 {
gen occ`num' = age_kid if siblingorder==`num'
egen sib`num' = mode(occ`num'), by(idhous12) minmode
}
*
drop occ*

foreach num of numlist 1(1)8 {
gen xage`num' = age`num'

}
*

foreach num of numlist 1(1)14 {
egen occ`num' = rowmax(sib1-sib6 xage1-xage8)
foreach num1 of numlist 1(1)8 {
replace xage`num1' = . if xage`num1'==occ`num'
}
foreach num2 of numlist 1(1)6 {
replace sib`num2' = . if sib`num2'==occ`num'
}
}
*
drop occ7-occ14 sib1-sib6 xage1-xage8


foreach x of numlist 1(1)8 {
replace siblingorder = siblingorder+1 if age`x'>age_kid & age`x'!=.
}
*

drop age1-age8

***Generate indicator for first born vs. the rest
gen sibling2 = 0 if inrange(siblingorder, 2, 10)
replace sibling2 = 1 if siblingorder==1

***Generate indicator for first born, second born and the rest
gen sibling3 = 1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 10)

***Generate indicator for first sample
gen year = 2012


***Generate age_father and age_mother
gen age_father = .
replace age_father = age_ref if sex_ref==1
replace age_father = age_spouse if sex_spouse==1

gen age_mother = .
replace age_mother = age_ref if sex_ref==2
replace age_mother = age_spouse if sex_spouse==2


***Rename before appending and change age to birthyear
rename idhous12 idhous
replace age_kid = 2012-age_kid
replace age_ref = 2012-age_ref
replace age_spouse = 2012-age_spouse
replace age_mother = 2012-age_mother
replace age_father = 2012-age_father
replace occ1 = occ1+1
replace occ2 = occ2+1
replace occ3 = occ3+1
replace occ4 = occ4+1
replace occ5 = occ5+1
replace occ6 = occ6+1
rename total_kids famsize

***Generate idpers_father and idpers_mother
gen idpers_father = .
replace idpers_father = idpers_ref if sex_ref==1
replace idpers_father = idpers_spouse if sex_spouse==1

gen idpers_mother = .
replace idpers_mother = idpers_ref if sex_ref==2
replace idpers_mother = idpers_spouse if sex_spouse==2

order idpers_kid idpers_ref idpers_spouse idpers_father idpers_mother idhous sex_kid age_kid sex_ref age_ref sex_spouse age_spouse age_mother age_father sample_kids nonsample_kids famsize siblingorder sibling2 sibling3 year occ1-occ6


***Add political outcomes for the kids
rename idpers_kid idpers
merge 1:1 idpers using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Data\W14_2012\shp12_p_user.dta", keepusing(p12p01)
drop if _merge==2
drop _merge
rename idpers idpers_kid


***Generate indicator for political interest
replace p12p01 = . if inrange(p12p01, -3, -1)
rename p12p01 pol_intr_kid


save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling2012.dta", replace

clear

use "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling1999.dta", clear

foreach num of numlist 2000(1)2012 {
append using "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling`num'.dta"
}

save "D:\Data\ProjData\SiblingOrder\OtherSamples\SHPpooled.dta", replace

foreach num of numlist 1999(1)2012 {
erase "D:\Data\ProjData\SiblingOrder\OtherSamples\SHP\Temp\SHPsibling`num'.dta"
}
*

***Drop 3 obs in which the kid is much older than any of the parents
drop if inrange(age_kid, 1917, 1927)


***Make sure kid and parents have the same birth year across waves
foreach name in kid ref spouse father mother {
egen occ = max(age_`name'), by(idpers_`name')
drop age_`name'
rename occ age_`name'
}
*


***Drop households if mother not older than kid
gen occ = age_kid-age_mother
gen nocc = 1 if occ<1
bysort idhous: egen mocc = total(nocc)
keep if mocc==0
drop occ nocc mocc



*Make sure that all kids with info on turnout, polinterest and exteff have info for all years
foreach name in turnout_kid pol_intr_kid ext_eff_kid {
bysort idpers_kid: egen occ = mean(`name')
replace `name' = occ
drop occ
}

*/
***Keep only one obs per kid
bysort idpers_kid: gen occ = _n
keep if occ==1
drop occ



***Save file for pooled analysis
keep idpers_mother turnout_kid siblingorder sibling2 age_kid sex_kid pol_intr_kid ext_eff_kid
rename idpers_mother famid
rename turnout_kid turnout
rename siblingorder birthorder
rename sibling2 birthorder_dum
rename sex_kid female
rename age_kid age
rename pol_intr_kid polinterest
rename ext_eff_kid exteff
replace female = female-1
replace age = 2013-age //age in 2013
gen sample = "SHP"
keep if inrange(birthorder, 1, 5)
qui: reghdfe turnout birthorder_dum female i.age, absorb(famid) cluster(famid)
sum turnout if e(sample)
gen turnout_average = r(mean)

save "D:\Data\ProjData\SiblingOrder\ReplicationSHP data 191128.dta", replace

